/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.hhx.web;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.activiti.engine.impl.util.json.JSONObject;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.mapper.JsonMapper;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.hhx.entity.HhxUser;
import com.thinkgem.jeesite.modules.hhx.service.HhxUserService;

/**
 * 单表生成Controller
 * @author zmy
 * @version 2019-04-08
 */
@Controller
@RequestMapping(value = "${adminPath}/hhx/hhxUser")
public class HhxUserController extends BaseController {

	@Autowired
	private HhxUserService hhxUserService;
	
	@ModelAttribute
	public HhxUser get(@RequestParam(required=false) String id) {
		HhxUser entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = hhxUserService.get(id);
		}
		if (entity == null){
			entity = new HhxUser();
		}
		return entity;
	}
	
	@RequiresPermissions("hhx:hhxUser:view")
	@RequestMapping(value = {"list", ""})
	public String list(HhxUser hhxUser, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<HhxUser> page = hhxUserService.findPage(new Page<HhxUser>(request, response), hhxUser); 
		model.addAttribute("page", page);
		return "modules/hhx/hhxUserList";
	}

	@RequiresPermissions("hhx:hhxUser:view")
	@RequestMapping(value = "form")
	public String form(HhxUser hhxUser, Model model) {
		model.addAttribute("hhxUser", hhxUser);
		return "modules/hhx/hhxUserForm";
	}

	@RequiresPermissions("hhx:hhxUser:edit")
	@RequestMapping(value = "save")
	public String save(HhxUser hhxUser, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, hhxUser)){
			return form(hhxUser, model);
		}
		hhxUserService.save(hhxUser);
		addMessage(redirectAttributes, "保存金融用户成功");
		return "redirect:"+Global.getAdminPath()+"/hhx/hhxUser/?repage";
	}
	
	@RequiresPermissions("hhx:hhxUser:edit")
	@RequestMapping(value = "delete")
	public String delete(HhxUser hhxUser, RedirectAttributes redirectAttributes) {
		hhxUserService.delete(hhxUser);
		addMessage(redirectAttributes, "删除金融用户成功");
		return "redirect:"+Global.getAdminPath()+"/hhx/hhxUser/?repage";
	}
	
	@RequestMapping(value="wxlogin")
	@ResponseBody
	public String wxlogin(HttpServletRequest request, HttpServletResponse response,String username,String password) {
		String psd = hhxUserService.getPsd(username);
		Map<String, String> map = new HashMap<String, String>();
		if(psd.equals(password)) {
			map.put("result", "true");
			map.put("msg", "登录成功");
        	
		}else {
			map.put("result", "false");
			map.put("msg", "登录失败");
		}
		return renderString(response, map);

	}
	
	@RequestMapping(value = {"wxlist", ""})
	public String wxlist(HhxUser hhxUser, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<HhxUser> page = hhxUserService.findPage(new Page<HhxUser>(request, response), hhxUser); 
//		model.addAttribute("page", page);
		return renderString(response, page);
	}
	
	@RequestMapping(value = {"wxget", ""})
	public String wxget(HttpServletRequest request, HttpServletResponse response, String id) {
		HhxUser entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = hhxUserService.get(id);
		}
		if (entity == null){
			entity = new HhxUser();
		}
		return renderString(response, entity);
	}
	
	@RequestMapping(value = {"wxgetInfo", ""})
	public String wxgetInfo(HttpServletRequest request, HttpServletResponse response,String username) {
		
		HhxUser entity = null;
		if (StringUtils.isNotBlank(username)){
			entity = hhxUserService.getInfoByName(username);
		}
		if (entity == null){
			entity = new HhxUser();
		}
		return renderString(response, entity);

	}
	
	@RequestMapping(value = {"wxregister", ""})
	public String wxregister(HttpServletRequest request, HttpServletResponse response,String username,String password,String companyname,String webaddress,String address,String category,String range,String amt,String description,String tel,String truename,String idcard,String job) {
		String id = UUID.randomUUID().toString().replace("-", "");
		int result = hhxUserService.register(id,username,password,companyname,webaddress,address,category,range,amt,description,tel,truename,idcard,job);
		Map<String, String> map = new HashMap<String, String>();
		if (result>0) {
			map.put("result","true");
			map.put("msg","注册成功");
		}else {
			map.put("result","false");
			map.put("msg","注册失败");
		}
		return renderString(response, map);

	}
	
	@RequestMapping(value = {"wxupdate", ""})
	public String wxupdate(HttpServletRequest request, HttpServletResponse response,String id) {

		int result = hhxUserService.updateuser(id);
		Map<String, String> map = new HashMap<String, String>();
		if (result>0) {
			map.put("result","true");
			map.put("msg","修改成功");
		}else {
			map.put("result","false");
			map.put("msg","修改失败");
		}
		return renderString(response, map);

	}

}